
package com.jf.cloud.distribution.controller.app;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.distribution.config.WxConfig;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 *
 * @author zz
 * @date 2021-08-12 10:46:10
 */
@RestController("appDistributionQrCodeController")
@RequestMapping("/ua/distribution_qrcode")
@Tag(name = "分销生成二维码接口")
public class DistributionQrCodeController {

    @Autowired
    private WxConfig wxConfig;

    @GetMapping("/invitation")
    @Operation(summary = "分销员邀请二维码" , description = "分销员的邀请二维码带有cardNo")
    @Parameters({
            @Parameter(name = "scene", description = "二维码携带的参数" , required = true),
            @Parameter(name = "page", description = "获取二维码后跳转的页面" , required = true)
    })
    public ResponseEntity<FileSystemResource> qrCodeCreate(String page, String scene) {
        File file = null;
        try {
            // 只有发布到线上了，正式版才能调用成功，否则错误码为：41030
            file = wxConfig.getWxMaService().getQrcodeService().createWxaCodeUnlimit(scene, page);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new LuckException("获取分销员邀请二维码失败!");
        }
        return ResponseEntity.ok(new FileSystemResource(file));
    }

}
